package com.micro.modual.service.impl;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.Seckill;
import com.micro.modual.entity.SeckillActivitySence;
import com.micro.modual.extension.SeckillActivitySenceExtension;
import com.micro.modual.mapper.SeckillActivitySenceMapper;
import com.micro.modual.mapper.SeckillDetailMapper;
import com.micro.modual.mapper.SeckillMapper;
import com.micro.modual.model.bo.seckillactivitysence.SeckillActivitySenceBO;
import com.micro.modual.model.dto.seckillactivitysence.ActivitySenceDTO;
import com.micro.modual.service.SeckillActivitySenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>秒杀活动场次关联表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("seckillActivitySenceService")
public class SeckillActivitySenceServiceImpl extends AbstractBasicsServiceImpl<SeckillActivitySence,Long> implements SeckillActivitySenceService {

    @Autowired
    private SeckillActivitySenceMapper seckillActivitySenceMapper;

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private SeckillDetailMapper seckillDetailMapper;

    /**
     * <h2>新增秒杀活动场次关联表详情</h2>
     * @param seckillActivitySenceBO {@link SeckillActivitySenceBO} 秒杀活动场次关联表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSeckillActivitySence(SeckillActivitySenceBO seckillActivitySenceBO) {
        List<SeckillActivitySence> seckillActivitySenceList = SeckillActivitySenceExtension.convertAddSeckillActivitySence(seckillActivitySenceBO.getSeckillCode(),seckillActivitySenceBO.getSenceCodeList());
        seckillActivitySenceMapper.insertListKeySelectiveMapper(seckillActivitySenceList);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }


    /**
     * <h2>根据主键编号删除秒杀活动场次关联表信息</h2>
     * @param id  秒杀活动场次关联表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSeckillActivitySence(Long id) {
        seckillActivitySenceMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJsonData<List<ActivitySenceDTO>> findActivitySenceList(String seckillCode) {
        List<Map<String,Object>> seckillActivitySenceList = seckillActivitySenceMapper.findActivitySenceList(seckillCode);
        List<ActivitySenceDTO> activitySenceDTOList = JacksonUtil.getList(JacksonUtil.toJson(seckillActivitySenceList),ActivitySenceDTO.class);
        return new RespJsonData<List<ActivitySenceDTO>>(activitySenceDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJson removeActivitySence(Long id) {
        SeckillActivitySence seckillActivitySence = seckillActivitySenceMapper.selectByPrimaryKey(id);
        Seckill seckill = seckillMapper.findSeckillByCode(seckillActivitySence.getSeckillCode());
        if(seckill.getSeckillStatus().equals(MicroConstant.StatusExecution.IN_PROGRESS)){
            throw new BizException(MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_DELETE.getResponseCode(),MicroResponseCode.HAS_IN_PROGRESS_SECKILL_NOT_ALLOW_DELETE.getResponseMessage());
        }
        seckillDetailMapper.deleteSeckillDetail(seckillActivitySence.getSeckillCode(),seckillActivitySence.getSceneCode());
        seckillActivitySenceMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

}
